
local meta = FindMetaTable( "Player" );

function meta:PrintBlueMessage( text )

	umsg.Start( "PBM", self );
		umsg.String( text );
	umsg.End();

end

function meta:SendPM( name, msg )

	local result, ret = SearchPlayerName( name );
	
	if( result ) then
	
		local rec = RecipientFilter();
		
		if( self ~= ret ) then
		
			rec:AddPlayer( self );
			
		end
		
		rec:AddPlayer( ret );
	
		umsg.Start( "PMM", rec );
			umsg.String( "[PM]" .. self:RPNick() .. ": " .. msg ); 
		umsg.End();
	
	else
	
		self:PrintBlueMessage( ret );
	
	end

end

function meta:SayLocalChat( text, area, msg, nonamefilter, nosemicolon, radio )

	if( not self:Alive() ) then
		return;
	end

	local tbl = ents.FindInSphere( self:GetPos(), area );
	
	local rec = RecipientFilter();

	if( nonamefilter ) then
	
		for k, v in pairs( tbl ) do
		
			if( v:IsPlayer() ) then
			
				rec:AddPlayer( v );
				
				if( nosemicolon ) then
					
					v:PrintMessage( 2, self:GetRPName() .. text );
			
				else
				
					v:PrintMessage( 2, self:GetRPName() .. ": " .. text );
				
				end
			
			end
			
		end
	
		umsg.Start( msg, rec );
			umsg.String( self:GetRPName() );
			umsg.String( text );
		umsg.End();
	
	else
	
		local descname = "[" .. string.sub( self:GetPlayerPhysDesc(), 1, 25 ) .. "..]";
	
		local rec2 = RecipientFilter();
	
		for k, v in pairs( tbl ) do
		
			if( v:IsPlayer() ) then
			
				if( ( self:GetTable().Recognized[v:SteamID() .."+" .. v:GetPlayerMySQLCharID()] and v:GetTable().Recognized[self:SteamID() .. "+" .. self:GetPlayerMySQLCharID()] ) or self == v ) then
				
					rec:AddPlayer( v );
					
					if( nosemicolon ) then
					
						v:PrintMessage( 2, self:GetRPName() .. text );
					
					else
					
						v:PrintMessage( 2, self:GetRPName() .. ": " .. text );
				
					end
		
				else
				
					rec2:AddPlayer( v );
					
					if( nosemicolon ) then
					
						v:PrintMessage( 2, descname .. text );
					
					else
					
						v:PrintMessage( 2, descname .. ": " .. text );
				
					end
				
				end
			
			end
			
		end	
		
		umsg.Start( msg, rec );
			umsg.String( self:GetRPName() );
			umsg.String( text );
		umsg.End();
		
		umsg.Start( msg, rec2 );
			umsg.String( descname );
			umsg.String( text );
		umsg.End();	
	
		if( radio ) then
		
			local f = function() 
		
				local rec = RecipientFilter();
				local rec2 = RecipientFilter();
				
				for k, v in pairs( player.GetAll() ) do
				
					if( v:GetPlayerCurRadioFreq() == self:GetPlayerCurRadioFreq() ) then
					
						if( ( self:GetTable().Recognized[v:SteamID() .."+" .. v:GetPlayerMySQLCharID()] and v:GetTable().Recognized[self:SteamID() .. "+" .. self:GetPlayerMySQLCharID()] ) or self == v ) then
						
							rec:AddPlayer( v );
							v:PrintMessage( 2, "@" .. v:GetPlayerCurRadioFreq() .. " - "  .. self:GetRPName() .. ": " .. text );
						
						else
						
							rec2:AddPlayer( v );
							v:PrintMessage( 2, "@" .. v:GetPlayerCurRadioFreq() .. " - "  .. descname .. ": " .. text );
						
						end
					
					end
				
				end
				
				umsg.Start( "r", rec );
					umsg.String( self:GetRPName() );
					umsg.String( text );
				umsg.End();
				
				umsg.Start( "r", rec2 );
					umsg.String( descname );
					umsg.String( text );
				umsg.End();	
			
			end
			
			timer.Simple( .2, f );	
				
		end
	
	end

end

function GM:PlayerSay( ply, text )

	if( ply:GetTable().CanNextChat > CurTime() ) then
	
		return "";
	
	end
	
	ply:GetTable().CanNextChat = CurTime() + .3;

	local correctcmd = nil;
	local crntlen;
	
	for k, v in pairs( ChatCommands ) do
	
		local len = string.len( v.cmd );
		local chatsub = string.sub( text, 1, len );
		
		if( string.lower( chatsub ) == string.lower( v.cmd ) ) then
		
			if( correctcmd ) then
			
				if( crntlen < len ) then
				
					correctcmd = k;
					crntlen = len;
				
				end
			
			else
			
				correctcmd = k;
				crntlen = len;
		
			end
		
		end
	
	end

	if( correctcmd ) then
	
		local ret = ( ChatCommands[correctcmd].cb( ply, ChatCommands[correctcmd].cmd, string.sub( text, crntlen + 1 ) ) or "" );	
	
		if( ret ~= "" and ret ~= " " ) then
		
			ply:SayGlobalChat( ret );
			return "(" .. ply:RPNick() .. ") " .. ret;
			
		end
		
		return "";
	
	elseif( string.sub( text, 1, 1 ) == "/" ) then
	
		return "";
	
	end
	
	ply:SayLocalChat( text, 180, "ic" );
	
	return "";

end